#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "uart.h"

extern uint8_t command;
uint8_t state = 0;
#define OFFSET						 (0x00003C00)
#define DOWNLOAD_FLAG_ADDR (0x0801FC00)

int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_SetVectorTable(NVIC_VectTab_FLASH,OFFSET);
	Delay_Init();
	Led_Init();
	Uart4_Init(115200);
	while(1)
	{
		if(command != 0)
		{
			if(command == 0x41)
			{
				__set_FAULTMASK(1);
				FLASH_Unlock();
				while(FLASH_ErasePage(DOWNLOAD_FLAG_ADDR) != FLASH_COMPLETE);
				FLASH_Lock();
				NVIC_SetVectorTable(NVIC_VectTab_FLASH,0);
				NVIC_SystemReset();
			}
			else if(command == 0x42)
			{
				Led_On(1);
			}
			else if(command == 0x43)
			{
				Led_On(0);
			}
			command = 0;
		}
		
		
		
	}

}
